﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.BusinessModel.DAL;
using System.Web.Security;

namespace iLangl.BusinessModel.Managers
{
	public class ActionManager : BaseManager
	{
		#region Singelton Pattern
		private ActionManager()
		{
		}

		private static ActionManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the CompanyManager.
		/// </summary>
		public static ActionManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new ActionManager();
						}
					}
				}
				return instance;
			}
		}
		#endregion

		public void PrepareAction(String companyId, ISecurityContext action)
		{
			PluginLicense pl = PluginManager.Instance.GetLicenseByCompanyId(companyId);

			PrepareAction(pl, action);
		}

		public void PrepareAction(PluginLicense pl, ISecurityContext action)
		{

			MembershipUser user = AuthenticationManager.Instance.GetUserId(pl.UserAccountId);
			PrepareAction(
				pl, 
				user, 
				action);
		}

		public void PrepareAction(PluginLicense pl, MembershipUser user, ISecurityContext action)
		{
			action.PluginLiceneKey = pl.ID;
			if (pl.Contact != null)
			{
				action.ContactId = pl.Contact.Id;
				
				if(pl.Contact.Company != null)
					action.CompanyId = pl.Contact.Company.Id;	
			}
			
			
			action.SessionId = AuthenticationManager.Instance.CreateSession((Guid)user.ProviderUserKey);
		}
	}
}
